package org.checkboo.hopefullyengine.text;

import org.checkboo.hopefullyengine.sceneelements.Texture;
import org.lwjgl.opengl.GL11;

public class Font {
	private String name;
	private Texture font;
	private int listBase;
	
	public Font(String name, Texture fontTex){
		this.name = name;
		this.font = fontTex;
		this.listBase = -1;
	}
	
	private void buildList () {
		float   cx;                                             // Holds Our X Character Coord
        float   cy;                                             // Holds Our Y Character Coord

        listBase = GL11.glGenLists(256);                            // Creating 256 Display Lists
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.font.getTexturePointer());     // Select Our Font Texture
        for (int i=0;i<256;i++) {                               // Loop Through All 256 Lists
            cx = ((float)(i % 16)) / 16.0f;                     // X Position Of Current Character
            cy = ((float)(i / 16)+1) / 16.0f;                     // Y Position Of Current Character

            GL11.glNewList(listBase + i, GL11.GL_COMPILE);          // Start Building A List
            GL11.glBegin(GL11.GL_QUADS);                        // Use A Quad For Each Character
            GL11.glTexCoord2f(cx, cy - 0.0625f);            // Texture Coord (Bottom Left)
            GL11.glVertex2i(0, 16);                              // Vertex Coord (Bottom Left)
            GL11.glTexCoord2f(cx + 0.0625f, cy - 0.0625f);  // Texture Coord (Bottom Right)
            GL11.glVertex2i(16,16);                              // Vertex Coord (Bottom Right)
            GL11.glTexCoord2f(cx + 0.0625f, cy);            // Texture Coord (Top Right)
            GL11.glVertex2i(16,0);                             // Vertex Coord (Top Right)
            GL11.glTexCoord2f(cx, cy);                      // Texture Coord (Top Left)
            GL11.glVertex2i(0, 1);                             // Vertex Coord (Top Left)
            GL11.glEnd();                                       // Done Building Our Quad (Character)
            GL11.glTranslatef(10.0f, 0.0f, 0.0f);               // Move To The Right Of The Character
            GL11.glEndList();                                   // Done Building The Display List
        }                                                       // Loop Until All 256 Are Built          
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getListBase() {
		if(listBase == -1)
			buildList();
		return listBase;
	}

	public Texture getFont() {
		return font;
	}
}
